package cn.itcast.controller;

import cn.itcast.domain.Courier;
import cn.itcast.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class CourierController {
    //注入Service
    @Autowired
    private CourierService courierService;


    /**
     * 添加
     * @param courier
     * @return
     */
    @RequestMapping("/courier_save")
    public String save(Courier courier) {
        System.out.println(courier);
        courierService.save(courier);
        //重定向到页面 默认是转发
        return "redirect:/pages/base/courier.html";

    }

    /**
     * 分页查询
     * @param page
     * @param rows
     * 加入Courier对象 实现模糊查询
     * @return
     */
    @RequestMapping("/courier_pageQuery")
    @ResponseBody
    public Map courierpaggQuery(Courier courier,int page, int rows) {
        System.out.println("page=" + page + "rows=" + rows);
        Page<Courier> pageResult = courierService.courierpageQuery(courier,page, rows);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", pageResult.getTotalElements());
        hashMap.put("rows", pageResult.getContent());
        return hashMap;
    }

    /**
     * 作废
     * @param ids
     * @return
     */
    @RequestMapping("/courier_batchDel")
    public String courierBatchDel(@RequestParam(value="ids")String ids){
        //切割数组的中元素
        String[] split = ids.split(",");
        courierService.batchDel(split);
        return "redirect:/pages/base/courier.html";
    }

    /**
     * 还原
     * @param ids
     * @return
     */
    @RequestMapping("/courier_uptateById")
    public String uptateById(@RequestParam(value="ids")String ids){
        String[] split = ids.split(",");
        courierService.uptateById(split);
        return "redirect:/pages/base/courier.html";
    }
}